package com.wangpu.oa.web.servlet.rolemanege;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wangpu.oa.ao.RoleAo;
import com.wangpu.oa.dto.JSONResponse;
import com.wangpu.oa.dto.UserContext;
import com.wangpu.oa.entity.RoleEntity;
import com.wangpu.oa.exception.OAException;
import com.wangpu.oa.service.RoleService;
import com.wangpu.oa.service.impl.RoleServiceImpl;
import com.wangpu.oa.util.JSONUtil;

@WebServlet("/api/system/role/add")
public class AddServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		JSONResponse result=new JSONResponse();
		
		try {
			String code=req.getParameter("code");
			String name=req.getParameter("name");
			String remark=req.getParameter("remark");
			
			RoleAo ao=new RoleAo();
			ao.setCode(code);
			ao.setName(name);
			ao.setRemark(remark);
			
			RoleService service=new RoleServiceImpl();
			
			HttpSession session=req.getSession();
			UserContext uc=(UserContext) session.getAttribute("CURRENT-USER");
			RoleEntity r=service.addRole(ao, uc.getId());
			
			result.setCode("ok");
			result.setData(r.getId());
			
		}catch (OAException e) {
			result.setCode("0001");
			result.setMessage(e.getMessage());
		}catch (Exception e) {
			result.setCode("error");
			result.setMessage("系统繁忙，请稍候再试!");
			//打印异常信息，用于调试
			e.printStackTrace();
		}finally {
			
			Writer out=resp.getWriter();
			out.write(JSONUtil.toJsonString(result));
			out.close();
		}
	}
}
